/*
 * TableCellRenderer.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.gui.elements;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ColorUIResource;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author <a href="mailto:ss.paranoid@gmail.com">Paranoid</a>
 */
public class TableCellRenderer extends DefaultTableCellRenderer {
    
    // <editor-fold defaultstate="collapsed" desc=" fields needed to implement like default table cell renderer ">
    protected static final Color tableFocusCellForeground = UIManager.getColor("Table.focusCellForeground");
    protected static final Color tableFocusCellBackground = UIManager.getColor("Table.focusCellBackground");
    protected static final Color tableDropCellForeground = UIManager.getColor("Table.dropCellForeground");
    protected static final Color tableDropCellBackground = new ColorUIResource(192, 202, 209);
    protected static final Border tableFocusSelectedCellHighlightBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
    protected static final Border tableFocusCellHighlightBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
    // </editor-fold>
    
    protected static final Color ALTERNATIVE_BACKGROUND = new Color(240, 240, 240);
    protected static final Border noFocusBorder = DefaultTableCellRenderer.noFocusBorder;
    
    /** Creates a new instance of TableCellRenderer */
    public TableCellRenderer() {
        super();
    }
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        
        Color fg = null;
        Color bg = null;
        
        // <editor-fold defaultstate="collapsed" desc=" drop location checks ">
        JTable.DropLocation dropLocation = table.getDropLocation();
        if (dropLocation != null
                && !dropLocation.isInsertRow()
                && !dropLocation.isInsertColumn()
                && dropLocation.getRow() == row
                && dropLocation.getColumn() == column) {
            
            fg = tableDropCellForeground;
            bg = tableDropCellBackground;
            
            isSelected = true;
        }
        // </editor-fold>
        
        if (isSelected) {
            setForeground(fg == null ? table.getSelectionForeground() : fg);
            setBackground(bg == null ? table.getSelectionBackground() : bg);
        } else { // TODO may be will need to check how it is done in DefaultTableCellRenderer
            setForeground(table.getForeground());
            if (row % 2 != 0) {
                setBackground(ALTERNATIVE_BACKGROUND);
            } else {
                setBackground(table.getBackground());
            }
        }
        
        // <editor-fold defaultstate="collapsed" desc=" has focus ">
        if (hasFocus) {
            Border border = null;
            if (isSelected) {
                border = tableFocusSelectedCellHighlightBorder;
            }
            if (border == null) {
                border = tableFocusCellHighlightBorder;
            }
            setBorder(border);
            
            if (!isSelected && table.isCellEditable(row, column)) {
                Color col;
                col = tableFocusCellForeground;
                if (col != null) {
                    setForeground(col);
                }
                col = tableFocusCellBackground;
                if (col != null) {
                    setBackground(col);
                }
            }
        } else {
            setBorder(noFocusBorder);
        }
        // </editor-fold>
        
        setFont(table.getFont());
        setValue(value);
        return this;
    }

    @Override
    protected void setValue(Object value) {
        if (value != null) {
            String stringValue = value.toString();
            if (!stringValue.isEmpty()) {
                setText(stringValue);
                setToolTipText(stringValue);
                return;
            }
        }
        setText(null);
        setToolTipText(null);
    }
    
}
